'use strict';

app.controller('dept',['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        title:'机构列表信息',
        addTitle:'机构信息'
    };
    $scope.dataPageSize = 10;
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };
    function getDeptList(){
        toaster.getInfo('/a_yongan/selectlist/dept',null).then(function(data){
            if(data && data.data && data.data.data){
                $scope.zNodes =data.data.data;
                madeTree();
            }
        });
    }
    function getInfoList(){
        toaster.getInfo('/a_yongan/list/dept',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
                setTimeout(function(){
                    $('.table').trigger('footable_redraw');
                }, 100);
            }
        });
    }
    getDeptList();
    getInfoList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
        $scope.saveDateAll = data;
    }
    $scope.deptAdd = false;
    $scope.deptList = true;
    $scope.deptRow = {};
    $scope.showInfo = function(row){
        $scope.deptAdd = !($scope.deptAdd);
        $scope.deptList = !($scope.deptList);
        $scope.deptRow = row;
    };
    $scope.jumpToUrl = function() {
        $scope.deptRow = {'deptNumber':'JG'+$filter("date")(new Date(), "yyyyMMddHHmmss")};
        $scope.deptAdd = !($scope.deptAdd);
        $scope.deptList = !($scope.deptList);
        madeTree();
    };
    $scope.columns = [
        {
            title:'部门编号',
            show:true
        },
        {
            title:'部门名称',
            show:true
        },
        {
            title:'上级部门',
            show:true
        },
        {
            title:'创建时间',
            show:true
        },
        {
            title:'部门状态',
            show:true
        }
    ];
    $scope.saveDate=[];
    $scope.saveDateAll=[];
    $scope.doing_async = true;
    $scope.setting = {
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            beforeClick: showDetail
        }
    };
    function showDetail(treeId, treeNode, clickFlag){
        //此处进行列表筛选，显示子节点列表
        $scope.saveDate = [];
        $scope.saveDateAll.forEach(function(item){
            if(treeNode.id==1){
                $scope.saveDate.push(item);
            }else{
                if(treeNode.id==item.topId){
                    $scope.saveDate.push(item);
                }
            }
        });
        $scope.$apply();
        setTimeout(function(){
            $('.table').trigger('footable_redraw');
        }, 100);
    }
    $scope.zNodes =[];
    function madeTree(){
        setTimeout(function() {
            $.fn.zTree.init($("#mytree"), $scope.setting, $scope.zNodes);
        },100);
        $scope.doing_async = false;
    }
    madeTree();

    $scope.showSelectDept = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectDept.html',
            controller: 'selectDeptCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $('#'+id+'').val(selectedItem.name);
            $("#topId").val(selectedItem.id);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.saveInfo = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            var info = addDo();

        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    function checkItems(){
        var items = ['deptName','deptNum','pDept'];
        var itemsValue = ['部门名称','部门编号','上级部门'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    $scope.message = '是否确定保存？';
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }

    function addDo(){
        var notesdata = {};
        if(!$scope.deptRow.deptId){
            notesdata = {
                "deptId":"",
                "qy":$("#qy").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "deptNumber":$("#deptNumber").val(),
                "topId":$("#topId").val(),
                "topName":$("#topName").val(),
                "deptContent":$("#deptContent").val(),
                "deptName":$("#deptName").val()
            }
        }else{
            notesdata = {
                "deptId":$scope.deptRow.deptId,
                "qy":$("#qy").prop("checked"),
                "deptNumber":$("#deptNumber").val(),
                "topId":$("#topId").val(),
                "topName":$("#topName").val(),
                "deptContent":$("#deptContent").val(),
                "deptName":$("#deptName").val()
            }
        }
        var info="";
        toaster.postInfo('/a_yongan/add/dept',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.deptAdd = !($scope.deptAdd);
                $scope.deptList = !($scope.deptList);
                madeTree();
                getDeptList();
                getInfoList();
            }else{
                showMessage('seccess','','保存失败！');
                info = "请求错误！";
            }
        });
        return info;
    }
}]);